home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_07_05 / v7n5016a.txt < prev    next >
Text File  |  1989-04-08  |  1KB  |  73 lines

  1. Listing 1
  2.  
  3. class Frog {
  4. public:
  5.     Frog( char *p = "the anonymous frog")
  6.     {
  7.         name = p;
  8.         dist = 0;
  9.         printf( "BEGIN Frog %s\n", name);
  10.     }
  11.  
  12.     ~Frog()    { printf( "END Frog %s\n", name); }
  13.  
  14.     char *Name()            { return name; }
  15.  
  16.     virtual void jump(int x) { dist += x; }
  17.  
  18.     virtual void croak() { printf( "R-R-R-ibit\n"); }
  19.  
  20.     int distance()          { return dist; }
  21. private:
  22.     char *name;
  23.     int dist;
  24. };
  25.  
  26. Listing 2
  27.  
  28. class Newt : public Frog {
  29. public:
  30.     Newt( char *n) : (n) {}
  31.  
  32.     void jump( int x)
  33.     {
  34.         printf( "I'm a jumping Newt\n" );
  35.         // invoke base class function against
  36.         // this derived class object
  37.         Frog::jump( x);
  38.     }
  39. };
  40.  
  41. class Salamander : public Frog {
  42. public:
  43.     Salamander( char *n) : (n) {}
  44.  
  45.     void jump( int x)
  46.     {
  47.         printf( "I'm a jumping Salamander\n" );
  48.         Frog::jump( x);
  49.     }
  50. };
  51.  
  52. Listing 3
  53.  
  54. typedef int Truth;
  55. const int dim = 100;
  56.  
  57. class Stack[T] {        // Stack of T values
  58. public:
  59.     Stack()                 { sp = 0; }
  60.  
  61.     void push( T x)         { elt[sp++] = x; }
  62.  
  63.     T pop()                 { return elt[--sp]; }
  64.  
  65.     Truth isempty()         { return sp <= 0; }
  66.  
  67.     Truth isfull()          { return sp >= dim; }
  68. private:
  69.     T elt[dim];
  70.     int sp;         // sp is index of next slot available to push
  71. };
  72.  
  73.